<?php
// +----------------------------------------------------------------------
// | likeadmin快速开发前后端分离管理后台（PHP版）
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码，建议反馈是我们前进的动力
// | 开源版本可自由商用，可去除界面版权logo
// | gitee下载：https://gitee.com/likeshop_gitee/likeadmin
// | github下载：https://github.com/likeshop-github/likeadmin
// | 访问官网：https://www.likeadmin.cn
// | likeadmin团队 版权所有 拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeadminTeam
// +----------------------------------------------------------------------

namespace app\adminapi\logic;


use app\common\job\AutoGenerateImgJob;
use app\common\libs\ImgGenerate;
use app\common\model\ImgTask;
use app\common\logic\BaseLogic;
use think\facade\Db;
use think\facade\Queue;
use think\helper\Str;


/**
 * ImgTask逻辑
 * Class ImgTaskLogic
 * @package app\adminapi\logic
 */
class ImgTaskLogic extends BaseLogic
{


    /**
     * @notes 添加
     * @param array $params
     * @return bool
     * @author likeadmin
     * @date 2025/03/26 23:05
     */
    public static function add(array $params): bool
    {
        Db::startTrans();
        try {
            $no = generate_sn(ImgTask::class, 'no');
            ImgTask::create([
                'tb_worktask_id' => json_encode($params['tb_worktask_id']) ,
                'no'             => $no,
                'admib_id'       => request()->adminInfo['admin_id'],
            ]);

            Queue::push(AutoGenerateImgJob::class, [
                'no' => $no,
            ]);
            Db::commit();
            return true;
        } catch (\Exception $e) {
            Db::rollback();
            self::setError($e->getMessage());
            return false;
        }
    }


    /**
     * @notes 编辑
     * @param array $params
     * @return bool
     * @author likeadmin
     * @date 2025/03/26 23:05
     */
    public static function edit(array $params): bool
    {
        Db::startTrans();
        try {
            ImgTask::where('id', $params['id'])->update([
                'tb_worktask_id' => $params['tb_worktask_id'],
                'no'             => $params['no'],
                'status'         => $params['status'],
                'admib_id'       => $params['admib_id'],
                'path'           => $params['path']
            ]);

            Db::commit();
            return true;
        } catch (\Exception $e) {
            Db::rollback();
            self::setError($e->getMessage());
            return false;
        }
    }


    /**
     * @notes 删除
     * @param array $params
     * @return bool
     * @author likeadmin
     * @date 2025/03/26 23:05
     */
    public static function delete(array $params): bool
    {
        return ImgTask::destroy($params['id']);
    }


    /**
     * @notes 获取详情
     * @param $params
     * @return array
     * @author likeadmin
     * @date 2025/03/26 23:05
     */
    public static function detail($params): array
    {
        return ImgTask::findOrEmpty($params['id'])->toArray();
    }

    public static function generateByNo(string $no)
    {
        /**
         * @var ImgTask $imgTask
         */
        $imgTask = ImgTask::where('no', $no)->findOrFail();
        // 原始图片的路径
        $originalImagePath = public_path() . 'jietu/source/tpl.png';
        $taskIdArr = json_decode($imgTask->tb_worktask_id, true);
        $dirNname = Str::random(10);
        foreach ($taskIdArr as $item) {
            $tbWorkTask = TbWorktaskLogic::info($item);
            $imgGenerate =  new ImgGenerate();

            $imgGenerate->loadTpl($originalImagePath);
            $imgGenerate->text($tbWorkTask['产品型号'], 250, 260,'18');
            $imgGenerate->text($tbWorkTask['产品IMEI'], 234, 670);
            $imgGenerate->text($tbWorkTask['产品IMEI'], 234, 710);
            $imgGenerate->text($tbWorkTask['产品IMEI'], 234, 750);
            $imgGenerate->text($tbWorkTask['bujian'], 1160, 1255);
            $imgGenerate->text($tbWorkTask['KBB'], 1160, 1435);
            $imgGenerate->text($tbWorkTask['KBB'], 1160, 1565);
            $imgGenerate->text($tbWorkTask['KBB2'], 1160, 1655);
            $imgGenerate->text($tbWorkTask['KGB1'], 1160, 1695);
            $imgGenerate->text($tbWorkTask['KGB2'], 1160, 1735);
            $imgGenerate->text($tbWorkTask['受理日期'], 2360, 1195);
            $imgGenerate->text($tbWorkTask['受理日期'], 2360, 1415);
            $imgGenerate->text($tbWorkTask['受理日期'], 2360, 1645);
//        $imgGenerate->text($tbWorkTask['受理日期'], 234, 750);
            $sourceDir = $imgGenerate->save($dirNname,'output_image_'.$item);
        }
        $zipFileName = public_path() . 'jietu/'.$dirNname.'.zip';
        $zipFilePath = $imgGenerate->createZip($sourceDir, $zipFileName);

        $imgTask->path = env('APP.domain').'/jietu/'. $dirNname.'.zip';
        $imgTask->status = 1;
        $imgTask->save();
    }


}